package com.news.dao.impl;

import com.news.dao.CmsCommentDao;
import com.news.entity.Comment;
import com.news.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/3/29
 */
public class CmsCommentDaoImpl implements CmsCommentDao {

    private static Connection connection = JdbcUtils.getConnection();

    @Override
    public List<Map<String,Object>> getCommentWaitCheck(int pageNum, int pageSize) {
        List<Map<String,Object>> commentList = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT a.id,comment,username,a.is_show,title FROM cms_comment as a LEFT JOIN cms_article as b on a.article_id = b.id  order by a.id desc limit ?,?");
        try {
            PreparedStatement pst = connection.prepareStatement(sql.toString());
            pst.setInt(1, pageNum);
            pst.setInt(2, pageSize);
            ResultSet rs = pst.executeQuery();
            while(rs.next()){
                HashMap<String, Object> map = new HashMap<>(5);
                map.put("id", rs.getInt(1));
                map.put("comment", rs.getString(2));
                map.put("userName", rs.getString(3));
                map.put("isShow", rs.getInt(4));
                map.put("title", rs.getString(5));
                commentList.add(map);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return commentList;
    }

    @Override
    public int updateComment(Comment comment) {
        String sql = "UPDATE cms_comment SET is_show = ? WHERE id = ?";
        PreparedStatement pst = null;
        int result = 0;
        try {
            pst = connection.prepareStatement(sql);
            pst.setInt(1, comment.getIsShow());
            pst.setInt(2, comment.getId());
            result = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }

    @Override
    public int doComment(Comment comment)  {
        String sql = "INSERT INTO cms_comment (comment, is_show, user_id, username, article_id) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement pst = null;
        int result = 0;
        try {
            pst = connection.prepareStatement(sql);
            pst.setString(1, comment.getComment());
            pst.setInt(2, 0);
            pst.setInt(3, comment.getUserId());
            pst.setString(4,comment.getUsername());
            pst.setInt(5, comment.getArticleId());
            result = pst.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }


    @Override
    public List<Comment> getComments(int articleId)  {
        String sql = "SELECT * FROM cms_comment WHERE article_id = ? and is_show = 1";
        PreparedStatement pst = null;
        int result = 0;
        List<Comment> commentList = new ArrayList<>();
        try {
            pst = connection.prepareStatement(sql);
            pst.setInt(1, articleId);
            ResultSet resultSet = pst.executeQuery();
            while(resultSet.next()){
                Comment comment = new Comment(resultSet.getInt(1),
                        resultSet.getInt(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getString(5),
                        resultSet.getInt(6));
                commentList.add(comment);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return commentList;
    }
}
